<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>跨域请求+WebSocket</title>
    <script>
        const init = async () => {
            try {
                
                let response = await fetch(`http://127.0.0.1:8888/api/seller`);
                let list = await response.json();
            } catch (error) {

            }
        }
        init()

        
    </script>
</head>

<body>
<button id="connect">连接</button>
<button id="send" disabled="true">发送</button>
<h1>从服务端接收数据如下：</h1>
<div id="recv"></div>
<script>

    const connect = document.querySelector('#connect')
    const send = document.querySelector('#send')
    const recv = document.querySelector('#recv')
    let ws = null;
    connect.onclick = function(){
        //连接服务端
        ws = new WebSocket(`ws://127.0.0.1:9998`)
        // console.log(ws);
        ws.onopen = ()=>{
            console.log('连接服务端成功了...');
            send.disabled = false
        }
        ws.onclose = ()=>{
            console.log('连接服务端失败了...');
            send.disabled = true
        }
        ws.onmessage = (data)=>{
            console.log('接收到服务端发送过来的数据了');
            console.log(data);
            recv.innerHTML = data.data
        }
    }
    send.onclick = function(){
        // ws.send(JSON.stringify({ msg: "我是阿宝哥" }))
        // const obj = {
        //     action:'getData',
        //     socketType:'trendData',
        //     chartName:'trend',
        //     value:''
        // }
        const obj = {
            action:'fullScreen',
            socketType:'fullScreen',
            chartName:'trend',
            value:true
        }
        ws.send(JSON.stringify(obj))
    }
</script>
</body>

</html>